import store from "../../store/store";
import $ from 'jquery'
import {url} from '../../util/config'
import {success,error} from "../../util/responseStatus";
import {message} from "antd";
import login from "../login";
let exception = error
let config = require('../../util/config').config

function getNewLogin(values) {
    return insertFile(values)
}

export default function insertFile(values) {
    console.log("api insertFile",values)
    let newDate = null;
    //附件或者文件的url数组
    let urls = [];
    //附件或者文件的名称数组
    let fileNames = [];
    let value = null;
    let contentType = null;
    let suffix = null;
    //添加文件
    if(values.fileType=="file"){
        console.log("values",values)
        console.log("values.upload",values.upload)
        // for(let i=0;i<values.upload.length;i++){
        //     // eslint-disable-next-line no-unused-expressions
        //     let result = values.upload[i].response.result[0];
        //     urls.push(result.FILE_URL)
        //     fileNames.push(result.FILE_URL_NAME)
        // }
        newDate = new FormData();
        if(values.allowOverride==undefined){
            newDate.append("OVERRIDE","false")
        }else newDate.append("OVERRIDE",values.allowOverride)
            newDate.append("PARENT_FOLDER_TYPE",values.moduleType)
            newDate.append("PARENT_FOLDER_ID",values.moduleParentId)
        if(values.fileDesc==undefined){
            newDate.append("FILE_DESC","")
        }else newDate.append("FILE_DESC",values.fileDesc)
        if(values.new==undefined){
            newDate.append("NOTIFY_DATE",config.new_notify)
        }else newDate.append("NOTIFY_DATE",values.new)
        if(values.fileTopStatus==undefined){
            newDate.append("TOP_STATUS","N")
        }else newDate.append("TOP_STATUS",values.fileTopStatus)
        for(let i=0;i<values.fileList.length;i++){
            newDate.append("FILE",values.fileList[i]);
        }
        suffix = "FileList/InsertFile"
        contentType = false;
        // contentType = "multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA";
        // newDate = {
        //     PARENT_FOLDER_TYPE:values.moduleType,   //父级类型
        //     PARENT_FOLDER_ID:values.moduleParentId, //父级id
        //     FILE_NAME:values.fileName,
        //     FILE_DESC:values.fileDesc,
        //     FILE_TYPE:values.fileType,
        //     TOP_STATUS:values.fileTopStatus,
        //     FILE_URL:urls,
        //     FILE_URL_NAME:fileNames
        // }
    }
    //添加文章
    else if(values.fileType=="article"){

        if(values.upload!=undefined){
            for(let i=0;i<values.upload.length;i++){
                let result = values.upload[i].response.result[0];
                urls.push(result.FILE_URL)
                fileNames.push(result.FILE_URL_NAME)
            }
        }

        newDate = JSON.stringify({
            ARTICLE_NEW:values.new,
            PARENT_FOLDER_TYPE:values.moduleType,             //父级类型
            PARENT_FOLDER_ID:values.moduleParentId, //父级id
            ARTICLE_NAME:values.fileName,
            ARTICLE_CONTENT:values.content,
            ARTICLE_DESC:values.fileDesc,
            FILE_TYPE:values.fileType,
            TOP_STATUS:values.fileTopStatus,
            ATTACH_URL:urls,
            ATTACH_NAME:fileNames,
            ATTACH_NUM:urls.length
        })
        suffix = "FileList/InsertFileContent"
        contentType = "application/json"
        console.log("newDate",newDate)
    }
    //如果添加文件夹
    else if(values.fileType=="folder"){
        newDate = JSON.stringify({
            NOTIFY_DATE:values.new,
            PARENT_FOLDER_TYPE:values.moduleType,             //父级类型
            PARENT_FOLDER_ID:values.moduleParentId, //父级id
            FOLDER_NAME:values.fileName, //文件夹名称
            FOLDER_DESC:values.fileDesc,		//文件夹描述
            FILE_TYPE:values.fileType,		//区分文件类型： folder
            TOP_STATUS:values.fileTopStatus,	//是否置顶，默认false
        })
        suffix = "FileList/InsertFileContent"
        contentType = "application/json"
    }

    let token = localStorage.getItem("token")

    console.log("Component Modal InsertModalContent insert",values)

    //如果ajax添加成功，则回调下面内容
    $.ajax({
        async:false,
        headers: {
            'Accept': "application/json",
            'Authorization': token
        },
        url: url.api_url+suffix,
        type: 'POST',
        contentType: contentType,
        processData: false,
        data: newDate,
        success: function (result) {
            if(result.result=="Token過期請重新登錄"){
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                value=getNewLogin(values);
            }else {
                if(result.result==="ok"){
                    value = "ok";
                    message.success("上传成功");
                } else {
                    if(result.code==="400 "){
                        value = "error"
                        message.error(result.result[0].error)
                    } else if(result.code==="500 "){
                        for(let i=0;i<result.result.length;i++){
                            value = "ok";
                            let node = result.result[i];
                            message.error(node.FILE_NAME+": "+node.error)
                        }
                    }
                }
            }

        },
        error:function (error) {
            console.log(error.responseText)
            message.error("请求异常:"+error.responseText)
            value = "error"
        }
    })
    return value;
}
